package com.osmino.contacts.entity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.util.Log;

public final class RawContact {

	public static final String ID_COLUMN = ContactsContract.RawContacts._ID;
	public static final String DATA_RAW_CONTACT_ID_COLUMN = ContactsContract.Data.RAW_CONTACT_ID;
	public static final String CONTACT_ID_COLUMN = ContactsContract.RawContacts.CONTACT_ID;
	public static final String ACCOUNT_TYPE_COLUMN = ContactsContract.RawContacts.ACCOUNT_TYPE;
	public static final String ACCOUNT_NAME_COLUMN = ContactsContract.RawContacts.ACCOUNT_NAME;

	public final long id;
	public final long contactId;
	public final LocalAccount account;
	private final List<Phone> mPhones;
	private final List<Email> mEmails;
	private final List<Long> mGroupIds;

	public RawContact(Cursor cursor) {
		int columnIndex = cursor.getColumnIndex(ID_COLUMN);
		id = cursor.getLong(columnIndex);
		contactId = resolveContactId(cursor);
		String accountName = resolveAccountName(cursor);
		String accountType = resolveAccountType(cursor);
		account = new LocalAccount(accountType, accountName);
		StringBuilder sb = new StringBuilder();
		sb.append("accountType=").append(accountType);
		sb.append(", accountName=").append(accountName);
		Log.e(getClass().toString(), sb.toString());
		mPhones = new ArrayList<Phone>();
		mEmails = new ArrayList<Email>();
		mGroupIds = new ArrayList<Long>();
	}

	private static long resolveContactId(Cursor cursor) {
		int columnIndex = cursor.getColumnIndex(CONTACT_ID_COLUMN);
		return cursor.getLong(columnIndex);
	}

	private static String resolveAccountType(Cursor cursor) {
		int columnIndex = cursor.getColumnIndex(ACCOUNT_TYPE_COLUMN);
		return cursor.getString(columnIndex);
	}

	private static String resolveAccountName(Cursor cursor) {
		int columnIndex = cursor.getColumnIndex(ACCOUNT_NAME_COLUMN);
		return cursor.getString(columnIndex);
	}

	public List<Phone> getPhones() {
		return mPhones;
	}

	private void addPhone(Phone phone) {
		mPhones.add(phone);
		Collections.sort(mPhones);
	}

	public List<Email> getEmails() {
		return mEmails;
	}

	private void addEmail(Email email) {
		mEmails.add(email);
		Collections.sort(mEmails);
	}

	public List<Long> getGroupIds() {
		return mGroupIds;
	}

	private void addGroupId(long groupId) {
		mGroupIds.add(groupId);
	}

	public void attachData(Context context, Cursor cursor) {
		int columnIndex = cursor.getColumnIndex(ContactsContract.Data.MIMETYPE);
		String mimetype = cursor.getString(columnIndex);
		if (mimetype.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)) {
			Phone phone = new Phone(context, cursor);
			addPhone(phone);
		} else if (mimetype.equals(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)) {
			Email email = new Email(context, cursor);
			addEmail(email);
		} else if (mimetype.equals(ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)) {
			columnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID);
			Long groupId = cursor.getLong(columnIndex);
			addGroupId(groupId);
		}
	}
}
